package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.ClueRuleDetaiDTO;
import com.xbongbong.paas.pojo.dto.ClueRuleSaveDTO;
import com.xbongbong.paas.pojo.dto.CustomerRuleGetRemindTimeDTO;
import com.xbongbong.paas.pojo.dto.CustomerRuleSaveRemindTimeDTO;
import com.xbongbong.paas.pojo.dto.CustomerRuleUserListDTO;
import com.xbongbong.paas.pojo.vo.ClueListRuleVO;
import com.xbongbong.paas.pojo.vo.ClueRuleDetailVO;
import com.xbongbong.paas.pojo.vo.CustomerRuleRemindTimeVO;
import com.xbongbong.paas.pojo.vo.CustomerRuleUserListVO;
import com.xbongbong.paas.pojo.vo.PublicRuleItemListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.service.ClueRuleService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ClueErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.help.PublicSeniorRuleHelp;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author long
 * @version v1.0
 * @date 2020/12/4 10:55
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/cluePublicRule")
public class ClueRuleController {

    @Resource
    private ClueRuleService clueRuleService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private PublicSeniorRuleHelp publicSeniorRuleHelp;

    /**
     * 【web】规则列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<ClueListRuleVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = clueRuleService.getClueRuleList(baseDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】规则详情
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid ClueRuleDetaiDTO clueRuleDetailDTO, BindingResult br) {
        XbbResponse<ClueRuleDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = clueRuleService.getClueRuleDetail(clueRuleDetailDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 【web】保存规则
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid ClueRuleSaveDTO clueRuleSaveDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(clueRuleSaveDTO.getCorpid(), XbbRefTypeEnum.CLUE.getCode())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                response = clueRuleService.save(clueRuleSaveDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】保存公海池退回提醒时间
     */
    @RequestMapping(value = "/saveRemindTime", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveRemindTime(@RequestBody @Valid CustomerRuleSaveRemindTimeDTO customerRuleSaveRemindTimeDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(customerRuleSaveRemindTimeDTO.getCorpid(), XbbRefTypeEnum.CLUE.getCode())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                response = clueRuleService.saveRemindTime(customerRuleSaveRemindTimeDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】获取公海池退回提醒时间
     */
    @RequestMapping(value = "/getRemindTime", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRemindTime(@RequestBody @Valid CustomerRuleGetRemindTimeDTO customerRuleGetRemindTimeDTO, BindingResult br) {
        XbbResponse<CustomerRuleRemindTimeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = clueRuleService.getRemindTime(customerRuleGetRemindTimeDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】公海规则设置选择员工
     */
    @RequestMapping(value = "/userList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String userList(@RequestBody @Valid CustomerRuleUserListDTO customerRuleUserListDTO, BindingResult br) {
        XbbResponse<CustomerRuleUserListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = clueRuleService.userList(customerRuleUserListDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/refreshGainRateRedis", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        String corpid = baseDTO.getCorpid();
        String userId = baseDTO.getUserId();
        paasRedisHelper.removeValue(RedisPrefixConstant.CLUE_GAIN_RATE, corpid + "_" + userId);

        return JSON.toJSONString(new JSONObject());
    }

    @RequestMapping(value = "/rule/itemList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String ruleItemList(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<PublicRuleItemListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicRuleItemListVO setListVO = clueRuleService.clueItemList(baseDTO);
                response = new XbbResponse<>(setListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
